package cn.mapper;

import cn.entity.SysArea;
import org.apache.ibatis.annotations.*;
import tk.mybatis.mapper.common.Mapper;

import java.util.HashMap;
import java.util.List;

public interface SysAreaMapper extends Mapper<SysArea> {
    @Select("SELECT sa.*,saa.name parent_name FROM sys_area sa left join sys_area saa on sa.parent_id=saa.id where sa.id=#{id}")
    SysArea selectArea(Long id);

    @SelectProvider(type = SysAreaSqlProvider.class,method = "selectAreas")
    List<SysArea> selectAreas(HashMap<String,Object> map);

    @SelectProvider(type = SysAreaSqlProvider.class,method = "selectChangeTree")
    List<SysArea> selectChangeTree(Long id);

    @Select("select sa.parent_ids FROM sys_area sa left join sys_area saa on sa.parent_id=saa.id WHERE sa.id = #{newId}")
    String selectParentIds( String newId);

    @Update("update sys_area set parent_ids =REPLACE(parent_ids,#{parentIds},#{newId}),parent_id=REPLACE(parent_id,#{parentId},#{newParentId}) WHERE FIND_IN_SET(#{id},parent_ids)")
    int updateParentIds(SysArea areaParents);

    @InsertProvider(type = SysAreaSqlProvider.class,method = "insertBatch")
    int insertBatch(@Param("areas") List<SysArea> areas);
}